package com.yehangtech.designpattern.创建型_抽象工厂.adpater;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class CacheInvocationHandler implements InvocationHandler {

    private final ICacheAdapter iCacheAdapter;

    public CacheInvocationHandler(ICacheAdapter iCacheAdapter) {
        this.iCacheAdapter = iCacheAdapter;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?>[] parameterTypes = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++) {
            parameterTypes[i] = args[i].getClass();
        }
        return ICacheAdapter.class.getMethod(method.getName(), parameterTypes).invoke(iCacheAdapter, args);
        // todo：思考为什么不能用下面这一个？？
//        return method.invoke(iCacheAdapter,args);
    }
}
